package com.cgt.web.servlet;

import com.cgt.web.pojo.Teacher;
import com.cgt.web.service.TeaService;
import com.cgt.web.service.impl.TeaServiceImpl;
import com.cgt.web.util.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/teacher")
public class TeacherServlet extends HttpServlet {
    private TeaService teaService = new TeaServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("TeacherServlet.service");
        req.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        if(method == null || method.equals("")){
            method = "selectByPage";
        }
        switch (method){
//            case "selectAll":
//                selectAll(req,resp);
//                break;
            case "deleteById":
                deleteById(req,resp);
                break;
            case "addt":
                addt(req,resp);
                break;
            case "toUpdate":
                toUpdate(req,resp);
                break;
            case "update":
                update(req,resp);
                break;
            case "selectByPage":
                selectByPage(req,resp);
                break;
        }
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("TeacherServlet.selectByPage");
        String pageNo = req.getParameter("pageNo");
        String pageSize = req.getParameter("pageSize");
        if(pageNo == null || pageNo.equals("")){
            pageNo = "1";
        }
        if(pageSize == null || pageSize.equals("")){
            pageSize = "5";
        }
        PageInfo<Teacher> pageInfo = teaService.selectByPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
        System.out.println(pageInfo);
        req.setAttribute("pageInfo",pageInfo);
        req.getRequestDispatcher("teacher_list.jsp").forward(req,resp);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("TeacherServlet.update");
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String address = req.getParameter("address");

        Teacher teacher = new Teacher(Integer.parseInt(id),name,Integer.parseInt(age),address);
        teaService.update(teacher);
        resp.sendRedirect("/teacher?method=selectByPage");
    }

    private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("TeacherServlet.toUpdate");
        String id = req.getParameter("id");
         Teacher teacher = teaService.toUpdate(id);
        req.setAttribute("teacher",teacher);
        req.getRequestDispatcher("teacher_update.jsp").forward(req,resp);
    }

    private void addt(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("TeacherServlet.addt");
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String address = req.getParameter("address");
        Teacher teacher = new Teacher();
        teacher.setName(name);
        teacher.setAge(Integer.valueOf(age));
        teacher.setAddress(address);
        teaService.addt(teacher);
        resp.sendRedirect("/teacher?method=selectByPage");
    }

//    private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("TeacherServlet.selectAll");
//        List<Teacher> list = teaService.selectAll();
//        req.setAttribute("list",list);
//        req.getRequestDispatcher("teacher_list.jsp").forward(req,resp);
//    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("TeacherServlet.deleteById");
        String id = req.getParameter("id");
        teaService.deleteById(id);
        resp.sendRedirect("/teacher?method=selectByPage");
    }
}
